#include "configall.h"
#include <stdio.h>
#include <WinSock2.h>
#include <iostream>
#include <windows.h>
#include <malloc.h>
#include <stdlib.h>
#include "NioService.h"
#pragma comment(lib,"WS2_32.lib")


void write(ByteQueue* bq, const char* src, int len) {
	writeback(bq, src, len);
}
void read(ByteQueue* bq, char* src, int capacity) {
	readfront(bq, src, capacity);
}

/*
int internal_send(SOCKET sServer, SOCKADDR_IN addrClient, int addrClientLen, ByteQueue* txbuf, int len) {
	char msg[MAX_MSG_SIZE];
	int retVal;
	read(txbuf, msg, len);
	printf("%d", len);
	while (true) 
	{
		printf("sendbefore\n");
		retVal = sendto(sServer, msg, sizeof(msg), 0, (const sockaddr*)&addrClient, addrClientLen);
		printf("sendafter\n");
		if (retVal != SOCKET_ERROR)
			break;
	}
	return retVal;
}*/

int internal_send(SOCKET sServer, SOCKADDR_IN addrClient, int addrClientLen, char* msg_send, int len) {
	//char msg[MAX_MSG_SIZE];
	int retVal;
	//read(txbuf, msg, len);
	//printf("%d", len);
	
	retVal = sendto(sServer, msg_send, sizeof(msg_send), 0, (const sockaddr*)&addrClient, addrClientLen);
		
	return retVal;
}

int internal_recv(SOCKET* sServer, SOCKADDR_IN* addrClient, int addrClientLen, char* msg_recv) {
	//char msg[MAX_MSG_SIZE];
	int retVal;
	while (true) 
	{
		retVal = recvfrom(*sServer, msg_recv, sizeof(msg_recv), 0, (sockaddr*)addrClient, &addrClientLen);
		if (retVal != SOCKET_ERROR) break;
	}//write(rxbuf, msg, sizeof(msg)); 
	return retVal;
}

/*
int internal_send(SOCKET sServer, SOCKADDR_IN addrClient, int addrClientLen, char* msg_send, int len);
int internal_recv(SOCKET sServer, SOCKADDR_IN addrClient, int addrClientLen, char* msg_recv);
*/
